<?php
class Card {
    public $arrNumber = array();    
    public $arrStatusNumber = array();
    public $arrTypePowerUp = array();
    public $exp = array();
    function __construct($exp) {
        $this->CreateCard($exp);
    }
   static function swap($bytebuffer) {
        $exp = 1;
        $card = new Card($exp); 
        for ( $iRow = 0; $iRow < 9; $iRow++){
            for ($iColumn = 0; $iColumn < 6; $iColumn++ )
            {
                $card->arrNumber[$iRow][$iColumn] = DataUtil::readInt32($bytebuffer);
                $card->arrStatusNumber[$iRow][$iColumn] = DataUtil::readInt32($bytebuffer);
                $card->arrTypePowerUp[$iRow][$iColumn] = DataUtil::readInt32($bytebuffer);
                $card->exp[$iRow][$iColumn] = DataUtil::readInt32($bytebuffer);
            }
        }
        return $card;
   }
    public function getArrNumber()
    {
        return $this->arrNumber;
    }
    public function getArrStatusNumber()
    {
        return $this->arrStatusNumber;
    }
    public function getArrTypePowerUp()
    {
        return $this->arrTypePowerUp;
    }
     public function getArrExp ()
    {
         return $this->exp;
    }
    public function CreatArray($exp)
    {
        for ( $iRow = 0; $iRow < 6; $iRow++)
	{
                $array = array();
		if ($iRow == 0)
		{
			$array = FunctionGameLoto::createArrayRandomNumber( 9, 1, 9);
		}else if ($iRow ==5)
		{
			$array = FunctionGameLoto::createArrayRandomNumber( 9, 50, 60);
		}
		else
		{
			$array = FunctionGameLoto::createArrayRandomNumber( 9,  10*$iRow, (($iRow+1)*10 - 1));
		}
		for ($iColumn = 0; $iColumn < 9; $iColumn++ )
		{
                    $this->arrNumber[$iColumn][$iRow] = $array[$iColumn];
                    $this->arrStatusNumber[$iColumn][$iRow] = 0;
                    $this->arrTypePowerUp[$iColumn][$iRow] = -1;
                    $this->exp[$iColumn][$iRow] = $exp;
		}
	}        
    }
    public function InsertZero()
    {
        $arrZeroPos = array();
	for ( $iRow = 0; $iRow < 9; $iRow++)
	{
		$arrZeroPos = FunctionGameLoto::createArrayRandomNumber(2 , 0, 5);
		for ($iColumn = 0; $iColumn < 6; $iColumn++ )
		{
			if ($iColumn == $arrZeroPos[0] || $iColumn == $arrZeroPos[1])
			{
                                $this->arrNumber[$iRow][$iColumn]= 0;
                                $this->arrStatusNumber[$iRow][$iColumn] = 1;
                                 $this->exp[$iRow][$iColumn] = -1;
			}
		}
	}
    }
    public function CreateCard($exp)
    {
        $this->CreatArray($exp);
        $this->InsertZero();
	for ( $i = 0; $i < 3; $i++)
	{       $iPowerType = -1;
		$iTemp_Type = rand(1,100);
		if ($iTemp_Type >= 1 && $iTemp_Type <=40 )
		{
			$iPowerType = 1;
		}else if ($iTemp_Type > 40 && $iTemp_Type <=50 )
		{
			$iPowerType = 0;
		}else 
		{
			$iPowerType = rand(2,5);
		}
		$this->AddCPow($iPowerType);
	}
    }
    public function AddCPow($iPowType)
    {
        $arrNonZeroAndNonPower = array();
	$iSerialNumber = 0;
        $len = 0;
	for ( $iRow = 0; $iRow < 9; $iRow++)
	{
		for ($iColumn = 0; $iColumn < 6; $iColumn++ )
		{
			if (        $this->arrNumber[$iRow][$iColumn] != 0 
				&&  $this->arrStatusNumber[$iRow][$iColumn]  == 0
				&&  $this->arrTypePowerUp[$iRow][$iColumn]   ==-1)
			{
				$arrNonZeroAndNonPower[$len] = $iSerialNumber;
				$len++;
			}
			$iSerialNumber ++;
		}
	}
        $iSerialNumber2 = 0;
	$iPowPos = rand( 0, $len - 1);
	for ( $iRow = 0; $iRow < 9; $iRow++)
	{
		for ($iColumn = 0; $iColumn < 6; $iColumn++ )
		{
			if ($arrNonZeroAndNonPower[$iPowPos] == $iSerialNumber2)
			{       
                                $this->arrTypePowerUp[$iRow][$iColumn] = $iPowType;
				$this->exp[$iRow][$iColumn] = -1;
			}
			$iSerialNumber2 ++;
		}
	}
    }
    function getBinSize() {
       $size = 0;
       $size += 4 * 54;     
       $size += 4 * 54;     
       $size += 4 * 54;
       $size += 4 * 54;
       return $size;
    }
    function getBinary(){
	$buf = ByteBuffer::allocate( $this->getBinSize() ) ;	
        if(is_array($this->arrNumber) && is_array($this->arrStatusNumber) && is_array($this->arrTypePowerUp) && is_array($this->exp))
        {
            for ( $iRow = 0; $iRow < 9; $iRow++){
                for ($iColumn = 0; $iColumn < 6; $iColumn++ )
                {
                    DataUtil::writeInt32( $buf , $this->arrNumber[$iRow][$iColumn] ) ;
                    DataUtil::writeInt32( $buf , $this->arrStatusNumber[$iRow][$iColumn] ) ;
                    DataUtil::writeInt32( $buf , $this->arrTypePowerUp[$iRow][$iColumn] ) ;
                    DataUtil::writeInt32( $buf , $this->exp[$iRow][$iColumn] ) ;
                }
            }
        }
        return  $buf ->_array() ;
    }
}
?>